<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryForm.name"
        :placeholder="$t('role.column.name.placeholder.query')"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <el-button
        v-waves
        class="filter-item"
        icon="el-icon-search"
        @click="handleFilter"
      >{{ $t('common.action.search') }}</el-button>

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
      >{{ $t('common.action.add') }}</el-button>
    </div>

    <el-table :data="tableData" border fit highlight-current-row>
      <el-table-column prop="name" align="center" :label="$t('role.column.name.name')" />
      <el-table-column align="center" :label="$t('common.options')">
        <template slot-scope="{row}">
          <el-button size="mini" @click="handleEdit(row)">{{ $t('common.action.edit') }}</el-button>

          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >{{ $t('common.action.delete') }}</el-button>

          <el-button
            size="mini"
            type="primary"
            @click="handleSetRoute(row)"
          >{{ $t('role.action.setRoute') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="diglogTitleMap[dialogTitle]" :visible.sync="dialogFormVisible">
      <el-form
        ref="editForm"
        :rules="editRules"
        :model="editForm"
        label-position="left"
        label-width="80px"
      >
        <el-form-item :label="$t('role.column.name.name')" prop="name">
          <el-input v-model="editForm.name" :placeholder="$t('role.column.name.placeholder.edit')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('common.action.close') }}</el-button>
        <el-button
          type="primary"
          @click="dialogTitle==='add'?apiAddRole():apiModifyRole()"
        >{{ $t('common.action.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listRole, addRole, modifyRole, detailRole, deleteRole } from '@/api/system/role'
import waves from '@/directive/waves'

export default {
  directives: { waves },
  data() {
    return {
      tableData: [],
      queryForm: {
        name: ''
      },
      editForm: {
        id: undefined,
        name: undefined
      },
      editRules: {
        name: [{ required: true, message: this.$t('validate.notNull'), trigger: 'blur' }]
      },
      dialogFormVisible: false,
      dialogTitle: '',
      diglogTitleMap: {
        add: this.$t('role.diglogTitle.add'),
        edit: this.$t('role.diglogTitle.edit')
      }
    }
  },
  created() {
    this.apiListRole()
  },
  methods: {
    apiListRole() {
      listRole(this.queryForm).then(response => {
        this.tableData = response.data
      })
    },
    apiAddRole() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          addRole(this.editForm).then(response => {
            this.apiListRole()
            this.dialogFormVisible = false
            this.$notify({
              title: this.$t('notify.success'),
              message: this.$t('common.options_success'),
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    apiModifyRole() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          modifyRole(this.editForm).then(response => {
            this.apiListRole()
            this.dialogFormVisible = false
            this.$notify({
              title: this.$t('notify.success'),
              message: this.$t('common.options_success'),
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    apiDetailRole(id) {
      detailRole(id).then(response => {
        this.editForm = response.data
      })
    },
    apiDeleteRole(id) {
      deleteRole(id).then(response => {
        this.apiListRole()
        this.$notify({
          title: this.$t('notify.success'),
          message: this.$t('common.options_success'),
          type: 'success',
          duration: 2000
        })
      })
    },
    handleFilter() {
      this.apiListRole()
    },
    handleAdd() {
      this.dialogTitle = 'add'
      this.editForm = {}
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
    },
    handleEdit(row) {
      this.dialogTitle = 'edit'
      this.dialogFormVisible = true
      this.apiDetailRole(row.id)
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('common.confirm_delete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.action.confirm'),
        cancelButtonText: this.$t('common.action.cancel'),
        type: 'warning'
      }).then(() => {
        this.apiDeleteRole(row.id)
      })
    },
    handleSetRoute(row) {
      alert(row.id)
    }
  }
}
</script>
